package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.WhiteListParam;
import com.project.hqshorttaxi.entity.WhiteList;
import com.project.hqshorttaxi.mapper.WhiteListMapper;
import com.project.hqshorttaxi.service.WhiteListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WhiteListServiceImpl implements WhiteListService {

    @Resource
    private WhiteListMapper whiteListMapper;

    @Override
    public WhiteList getById(Integer id) {
        return whiteListMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(WhiteList entity) throws Exception {
        whiteListMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(WhiteList entity) throws Exception {
        whiteListMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        whiteListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<WhiteList> list(WhiteListParam param) {
        Example example = new Example(WhiteList.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getPlateNumber())) {
            c.andLike("plateNumber", "%" + param.getPlateNumber() + "%");
        }
        if (param.getExchange() != null) {
            c.andEqualTo("exchange", param.getExchange());
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("effectiveDate", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("effectiveDate", param.getEndTime());
        }
        example.orderBy("createTime").desc();
        List<WhiteList> list = whiteListMapper.selectByExample(example);
        return list;
    }
}
